<template>
  <div class="tp-box">
    <a-drawer
      :visible="props.visible"
      :width="props.width"
      :closable="props.closable"
      :destroyOnClose="props.destroyOnClose"
      @close="handleClose"
    >
      <template #title>
        <div class="title-detail">
          <Tooltip placement="top">
            <template #title>
              <span>{{ getTipTitle }}</span>
            </template>
            <h3>{{ getTitle }}</h3>
          </Tooltip>
          <a-space direction="vertical" :size="12">
            <!-- <a-range-picker v-model:value="value4" :format="dateFormat" /> -->
          </a-space>
          &nbsp;&nbsp;&nbsp;&nbsp;
        </div>
      </template>

      <!-- :scroll="{ x: 1500, y: 600 }" -->
      <a-table
        :columns="computedTableColumn"
        :data-source="computedTableData"
        :page-size-options="pageSizeOptions"
        :loading="loading"
        bordered
        :pagination="false"
        size="middle"
        :scroll="{ x: 'calc(700px + 50%)', y: props.detailTableHeight }"
      >
        <!-- <template #action>
          <a>action</a>
        </template> -->
      </a-table>
      <div class="pagination">
        <a-pagination
          show-size-changer
          v-model:current="pageIndex"
          v-model:pageSize="pageSize"
          :total="total"
          @show-size-change="onShowSizeChange"
        />
      </div>
    </a-drawer>
    <VillageDetail
      v-model:vvvisible="vvisible"
      title="抽屉标题"
      :width="1500"
      :detailTableHeight="detailTableHeight"
      :vclosable="true"
      :vdestroyOnClose="true"
      :data="detailData"
      :vvvdetailParams="vdetailParams"
      :vcurrentTitle="v_currentTitle"
      @close="handleVillageClose"
    >
      <!-- <p>抽屉内容</p> -->
    </VillageDetail>
  </div>
</template>
<script setup lang="ts">
  import { Tooltip } from 'ant-design-vue';
  import { ref, computed, onMounted, watch } from 'vue';
  import { getSeltUserList } from '/@/api/demo/system';
  import VillageDetail from '../component/checkDetailDrawer.vue';
  // 表格的 ref Dom名称
  const vvisible = ref(false);
  let detailData = ref<any[]>([]);
  let vdetailParams = ref({}); // 详情参数
  let detailTableHeight = ref<number>(0); // 表格高度
  let v_currentTitle = ref({});
  // 默认传过来的值q
  const props = defineProps({
    visible: {
      type: Boolean,
      default: true,
    },
    title: {
      type: String,
      default: '',
    },
    width: {
      type: Number,
      default: 256,
    },
    detailTableHeight: {
      type: Number,
      default: 600,
    },
    hzfs: {
      type: String,
      default: '',
    },
    closable: {
      type: Boolean,
      default: true,
    },
    data: {
      type: Array,
      default: () => [],
    },
    detailParams: {
      type: Object,
      default: () => {},
    },
    currentXzqdmname: {
      type: String,
      default: '',
    },
    currentTitle: {
      type: String,
      default: '',
    },
    wscolsList: {
      type: Array,
      default: () => [{ wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }],
    },
    destroyOnClose: {
      type: Boolean,
      default: true,
    },
    onClose: {
      type: Function as PropType<() => void>,
      default: () => {},
    },
  });
  let pageIndex = ref(1);
  let pageSize = ref(100);
  let loading = ref(false);
  const pageSizeOptions = ref<string[]>(['100', '200', '300']);
  let tableSource = ref<any>([]);
  let total = ref(0);
  const columns = [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      align: 'center',
      width: 80,
    },
    {
      title: '区名称',
      dataIndex: 'xzq',
      key: 'xzq',
      align: 'center',
      width: 80,
    },
    {
      title: '街/乡镇名称',
      dataIndex: 'town',
      key: 'town',
      align: 'center',
      width: 100,
    },
    {
      title: '村/社区名称',
      dataIndex: 'village',
      key: 'village',
      align: 'center',
      width: 170,
    },
    {
      title: '用户姓名',
      dataIndex: 'nickname',
      key: 'nickname',
      align: 'center',
      width: 250,
    },
    {
      title: '地址',
      dataIndex: 'address',
      key: 'address',
      align: 'left',
      width: 350,
    },
    {
      title: '房屋编号',
      dataIndex: 'number',
      key: 'number',
      align: 'center',
      width: 170,
      customCell: (row, index, column) => {
        let isNoHzyh = row.xzq !== '汇总' ? true : false;
        let style = 'cursor:pointer;text-decoration: underline; color: blue;';
        let isNoZero = row[column.key] !== 0 ? true : false;
        return {
          style: `${isNoHzyh === true && isNoZero ? style : ''}`,
          class: 'cell-hover',
          onClick: async () => {
            getDetailData(row, index, column);
          },
        };
      },
    },
    {
      title: '最近一次自查记录',
      dataIndex: 'pcsj',
      key: 'pcsj',
      align: 'center',
      width: 150,
    },
  ];
  // 计算属性，用于实时更新数据到页面上
  const computedTableColumn = computed(() => {
    let newColumn: any = [];
    newColumn = columns;
    return newColumn;
  });
  const computedTableData = computed(() => {
    console.log(tableSource.value);
    return tableSource.value;
  });
  const getTitle = computed(() => {
    // let str = `【${props.currentXzqdmname} 】-- ${props.currentTitle}`;
    let str = `【${props.currentTitle}】绑定的房屋信息`;
    if (str.length > 50) {
      str = str.substring(0, 50) + '...';
    }
    return str;
  });
  const getTipTitle = computed(() => {
    let str = `${props.currentTitle}`;
    return str;
  });
  watch(
    () => props.visible,
    (newVal) => {
      // console.log(`visible 变化了，新值：${newVal}，旧值：${oldVal}`);
      // 在这里执行你需要的操作
      if (newVal) {
        getTableData();
      }
    },
  );
  const getTableData = async () => {
    let params: any = {
      pageindex: pageIndex.value,
      pagesize: pageSize.value,
      ...props.detailParams,
    };
    loading.value = true;
    let res = await getSeltUserList(params);
    loading.value = false;
 
    let list = res.items;
    tableSource.value = list;
    total.value = res.total;

    // console.log('@resqqq', list);
  };
  //  调用 父组件传过来的方法
  const emit = defineEmits(['close']);
  // 导出
  const getDetailData = async (row: any, isNoHzyh: boolean, isNoZero: boolean) => {
    if (isNoHzyh && isNoZero) {
      let params: any = {
        town: row.town,
        fwbh: row.number,
        ...props.detailParams,
      };
      vdetailParams.value = params;
      v_currentTitle.value = row.number;
      showDrawer();
    }
  };

  // 打开抽屉
  function showDrawer() {
    vvisible.value = true;
  }

  const handleClose = () => {
    emit('close');
    pageIndex.value = 1;
    pageSize.value = 15;
    vvisible.value = false;
  };
  const handleVillageClose = () => {
    vvisible.value = false;
  };
  // 切换页码的时候更新  pageIndex 和  pageSize
  const onShowSizeChange = (pageIndex: number, pageSize: number) => {
    // antDesign  自动给  pageIndex 和 pageSize 赋值
    // pageIndex.value = pageIndex;
    // pageSize.value = pageSize;
    // console.log('@pageIndex,pageSize', pageIndex, pageSize);
  };
  watch(pageSize, () => {
    // console.log('pageSize', pageSize.value);
    getTableData();
  });
  watch(pageIndex, () => {
    // console.log('current', pageIndex.value);
    getTableData();
  });
  onMounted(() => {
    getTableData();
  });
</script>
<style lang="less" scoped>
  .tp-box {
    height: 100%;
  }
  .title-detail {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .pagination {
    margin-top: 5px;
    display: flex;
    justify-content: flex-end;
  }
</style>
